// pages/index/index.js
Page({
  data: {
    userName: '',
    currentDate: '',
    showReminder: false,
    hasBloodPressureRecords: false,
    hasMedicationRecords: false,
    latestBloodPressure: {},
    latestMedication: {},
    settings: {}
  },

  onLoad() {
    // 初始化页面数据
    this.initPageData();
    
    // 检查是否需要显示用药提醒
    this.checkReminderStatus();
    
    // 监听本地存储变化
    wx.onStorageChange(this.handleStorageChange);
  },

  onShow() {
    // 页面显示时刷新数据
    this.initPageData();
  },

  onUnload() {
    // 页面卸载时移除监听
    wx.offStorageChange(this.handleStorageChange);
  },

  initPageData() {
    // 获取用户设置
    const settings = wx.getStorageSync('settings') || {};
    
    // 获取血压记录
    const bloodPressureRecords = wx.getStorageSync('bloodPressureRecords') || [];
    
    // 获取用药记录
    const medicationRecords = wx.getStorageSync('medicationRecords') || [];
    
    // 格式化当前日期
    const currentDate = this.formatDate(new Date());
    
    this.setData({
      userName: settings.userName || '用户',
      currentDate: currentDate,
      settings: settings,
      hasBloodPressureRecords: bloodPressureRecords.length > 0,
      hasMedicationRecords: medicationRecords.length > 0,
      latestBloodPressure: bloodPressureRecords.length > 0 ? bloodPressureRecords[0] : {},
      latestMedication: medicationRecords.length > 0 ? medicationRecords[0] : {}
    });
  },

  checkReminderStatus() {
    // 获取当前时间
    const now = new Date();
    const currentHour = now.getHours();
    const currentMinute = now.getMinutes();
    
    // 获取设置的提醒时间
    const settings = wx.getStorageSync('settings');
    if (!settings.reminderTime) return;
    
    const [targetHour, targetMinute] = settings.reminderTime.split(':').map(Number);
    
    // 检查是否在提醒时间15分钟内
    const targetTime = targetHour * 60 + targetMinute;
    const currentTime = currentHour * 60 + currentMinute;
    
    // 检查是否在提醒时间后15分钟内
    if (currentTime >= targetTime && currentTime <= targetTime + 15) {
      // 检查今天是否已记录用药
      const medicationRecords = wx.getStorageSync('medicationRecords') || [];
      const today = now.toISOString().split('T')[0];
      
      const hasRecordToday = medicationRecords.some(record => 
        record.recordTime.startsWith(today)
      );
      
      if (!hasRecordToday) {
        this.setData({ showReminder: true });
      }
    }
  },

  handleStorageChange(e) {
    // 当本地存储变化时刷新数据
    if (e.key === 'bloodPressureRecords' || e.key === 'medicationRecords' || e.key === 'settings') {
      this.initPageData();
      this.checkReminderStatus();
    }
  },

  formatDate(date) {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    const weekDay = weekDays[date.getDay()];
    
    return `${year}年${month}月${day}日 ${weekDay}`;
  },

  goToBloodPressure() {
    wx.navigateTo({
      url: '/pages/blood-pressure/blood-pressure'
    });
  },

  goToMedication() {
    wx.navigateTo({
      url: '/pages/medication/medication'
    });
  }
})